<template>
  <div class="ingredient_manager">
    <div class="ingredient_title">{{ id ? '编辑' : '新增'}}用户</div>
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="账号" prop="user_name">
        <el-input
          v-model="form.user_name"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item v-if="!id" label="密码" prop="pass">
        <el-input
          v-model="form.pass"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="account">
        <el-input
          v-model="form.account"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="自我介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          :rows="5"
          maxlength="500"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="form.phone"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="微信" prop="vx">
        <el-input
          v-model="form.vx"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="QQ" prop="qq">
        <el-input
          v-model="form.qq"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="爱好" prop="hobby">
        <el-input
          v-model="form.hobby"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="form.sex"
          placeholder="请选择性别"
        >
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input
          v-model="form.weight"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="头像" prop="avatar">
      </el-form-item> -->
      
    </el-form>
    <div class="operation">
      <div class="cancel_btn" @click="cancel">取消</div>
      <div class="submit_btn" @click="submit">提交</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRoute } from 'vue-router'
import { addAccount, updateAccountById, getAccountById } from '@/views/settings/backend/api'

import addList from '@/components/addList/index.vue'

const rules = reactive({
  user_name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  pass: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  introduce: [{ required: true, message: '请输入介绍', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入电话', trigger: 'blur' }],
})

const resetFields = (formData) => {
  const data = formData || initForm
  form.value = { ...data }
  formRef.value.clearValidate()
}

let form = ref({})
const formRef = ref()
let id = ''

const initForm = {
  user_name: '',
  account: '',
  pass: '',
  introduce: '',
  phone: '',
  city: '',
  vx: '',
  qq: '',
  sex: '',
  hobby: '',
  weight: ''
}

onMounted(() => {
  const route = useRoute()
  id = route.query.id
  if (id) {
    getAccountById(id).then(res => {
      form.value = res.result
    })
  } else {
    form.value = { ...initForm }
  }
})


const submit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (id) {
        const params = { ...form.value, id }
        updateAccountById(params).then(() => {
          ElMessage({
            type: 'success',
            message: '修改成功'
          })
          cancel()
        })
      } else {
        form.value.name = form.value.user_name
        delete form.value.user_name
        addAccount(form.value).then(() => {
          ElMessage({
            type: 'success',
            message: '添加成功'
          })
          resetFields()
        })
      }
    }
  })
}

const cancel = () => {
  history.back()
}
</script>

<style lang="scss" scoped>
.ingredient_manager {
  padding: 20px;
  width: calc(100% - 50px);

  .ingredient_title {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 50px;
    border-left: 5px solid var(--el-color-primary);
    text-align: left;
    padding-left: 10px;
  }

  ::v-deep .el-form-item__label {
    display: block;
    font-weight: bold;
    text-align: left;
  }

  ::v-deep .el-form-item {
    display: block;
  }

  .operation {
    height: 50px;
    display: flex;
    justify-content: end;

    .cancel_btn {
      width: 100px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border: 1px solid var(--el-color-primary);
      border-radius: 5px;
      margin-right: 20px;
      cursor: pointer;
    }
    .submit_btn {
      width: 100px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      color: #fff;
      background-color: var(--el-color-primary);
      border-radius: 5px;
      cursor: pointer;
    }
  }
}
</style>